package com.tuanzi.loan.workflow.cmd;

/**
 * @author: Henry Yan
 */

import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.pvm.process.ProcessDefinitionImpl;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JumpActivityCmd implements Command<Object> {
	private String processInstanceId;
	private String targetActivityId;
	private String jumpOriginActivityId;

	public JumpActivityCmd(String processInstanceId, String activityId) {
		this(processInstanceId, activityId, "jump");
	}

	/**
	 * @param processInstanceId
	 *            流程实例id
	 * @param targetActivityId
	 *            跳转目标活动id
	 * @param jumpOriginActivityId
	 *            当前活动id
	 */
	public JumpActivityCmd(String processInstanceId, String targetActivityId, String jumpOriginActivityId) {
		this.processInstanceId = processInstanceId;
		this.targetActivityId = targetActivityId;
		this.jumpOriginActivityId = jumpOriginActivityId;
	}

	public Object execute(CommandContext commandContext) {
		log.info("流程实例[" + processInstanceId + "]执行回退：从节点[" + jumpOriginActivityId + "]退回到节点[" + targetActivityId + "]");

		ExecutionEntity executionEntity = commandContext.getExecutionEntityManager().findExecutionById(processInstanceId);
		executionEntity.destroyScope(jumpOriginActivityId);
		ProcessDefinitionImpl processDefinition = executionEntity.getProcessDefinition();
		ActivityImpl activity = processDefinition.findActivity(targetActivityId);
		executionEntity.executeActivity(activity);

		return executionEntity;
	}
}
